package com.uzhie.web.service.report;

import com.uzhie.web.dao.CatalogDAO;
import com.uzhie.web.dao.GpsDAO;
import com.uzhie.web.dao.VehicleDAO;
import com.uzhie.web.module.Catalog;
import com.uzhie.web.module.GpsRecord;
import com.uzhie.web.module.ReportLastPosition;
import com.uzhie.web.module.Vehicle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 报表处理服务
 * Created by lilongfei on 2018/7/23.
 */
@Service
public class LastPositionReportService implements ReportHandler {


    @Autowired
    private GpsDAO gpsDAO;
    @Autowired
    private VehicleDAO vehicleDAO;
    @Autowired
    private CatalogDAO catalogDAO;

    public void insertPosition(String rpId ,String handler){
        ReportLastPosition lastPosition = new ReportLastPosition();
    }


    /**
     * 通过车牌号查询车辆最后位置信息
     * @param vehicleNumber 车牌号
     * @param departIds 用户车辆管理范围（部门id集合）
     * @return
     */
    public List<ReportLastPosition> queryLastPositionReport(String vehicleNumber ,List<String> departIds ) {
        Vehicle vehicle = vehicleDAO.selectVehicleNumber(vehicleNumber);
        if (vehicle != null && departIds.contains(vehicle.getDepartId())) {
            GpsRecord record = gpsDAO.queryListBySIM(vehicle.getSimNumber());
            ReportLastPosition lastPosition = ReportLastPosition.transformGpsRecord(record, vehicle);
            List<ReportLastPosition> positions = new ArrayList<>();
            positions.add(lastPosition);

            return positions;
        }else
            return null;
    }

    public List<ReportLastPosition> queryLastPositionReport(String vehicleNumber ,String userId) {
        List<Catalog> catalogs =catalogDAO.selectCatalogByUserId(userId);
        List<String> userDepartId = new ArrayList<>();
        for (Catalog catalog:catalogs){
            userDepartId.add(catalog.getId());
        }
        Vehicle vehicle = vehicleDAO.selectVehicleNumber(vehicleNumber);
        if (vehicle != null && userDepartId.contains(vehicle.getDepartId())) {
            GpsRecord record = gpsDAO.queryListBySIM(vehicle.getSimNumber());
            ReportLastPosition lastPosition = ReportLastPosition.transformGpsRecord(record, vehicle);
            List<ReportLastPosition> positions = new ArrayList<>();
            positions.add(lastPosition);
            return positions;
        }else
            return new ArrayList<>();
    }



    @Override
    public void handlerTask() {

    }
}
